**---------------------------------------------------------------------------------
** Paper: "How do past repression and indoctrination affect redistributive preferences?"
** Authors: Lars Pelke
** Contact: lars.pelke@ipw.uni-heidelberg.de
** Journal: tba
** Replication: All tables and graphs in MANUSCRIPT

cd "insert your directory here"

***-----------------------------------------------------------*****
* Import Mulitple Imputated Datasets *

use "data\data1.dta", clear
mi import flongsep data_new2, using("data\data1.dta" "data\data2.dta" "data\data3.dta" "data\data4.dta" "data\data5.dta" "data\data6.dta" "data\data7.dta" "data\data8.dta" "data\data9.dta" "data\data10.dta") ///
 id(individual_id) imputed(education_3 unemployed income_deciles sex)
sleep 5000
mi convert wide
*with STAATA MP mi convert wide would be better due to faster estimation times


***-----------------------------------------------------------*****
* Data management + 

gen dataset = 1 
replace dataset = 2 if data =="EVS"
replace dataset = 3 if data =="ESS"
replace dataset = 4 if data =="ISSP"
replace dataset = 5 if data =="Latinobarometer"

tab dataset

sum s_far_Maddison_pop_estimate
sum v2x_polyarchy_5
sum gini_disp
sum s_far_Maddison_gdppc_1990_stm_5


* Drop missing variables *
drop if missing(s_far_Maddison_pop_estimate)
drop if missing(v2x_polyarchy_5)
drop if missing(gini_disp) 
* 9,956 observations deleted
drop if missing(s_far_Maddison_gdppc_1990_stm_5) 

* Check Cohorts at least 10 per cohort for each country

table cohortmatch5_15 country_name

bysort cohortmatch5_15 country_name: drop if _N<10

table cohortmatch5_15 country_name

sum s_far_Maddison_gdppc_1990_stm_5
*  1,012,329 observations

*center variables*

sum v2x_polyarchy, meanonly
gen v2x_polyarchy_c=v2x_polyarchy-r(mean)

sum v2x_polyarchy_5, meanonly
gen v2x_polyarchy_5_c =v2x_polyarchy_5-r(mean)

sum e_migdppcln, meanonly
gen e_migdppcln_c=e_migdppcln-r(mean)

sum s_far_Maddison_gdppc_1990_stm_5, meanonly
gen s_far_Maddison_gdppc_1990_stm_5c = s_far_Maddison_gdppc_1990_stm_5 - r(mean)

sum v2x_clphy_5, meanonly
gen v2x_clphy_5_c=v2x_clphy_5-r(mean)

sum v2exl_legitideol_5, meanonly
gen v2exl_legitideol_5_c=v2exl_legitideol_5-r(mean)

sum gini_disp, meanonly
gen gini_disp_c = gini_disp-r(mean)

sum s_far_Maddison_pop_estimate, meanonly
gen s_far_Maddison_pop_estimatec = s_far_Maddison_pop_estimate-r(mean)

drop v2x_polyarchy v2x_polyarchy_5 e_migdppcln s_far_Maddison_gdppc_1990_stm_5 v2x_clphy_5 v2exl_legitideol_5 gini_disp
rename (v2x_polyarchy_c v2x_polyarchy_5_c e_migdppcln_c s_far_Maddison_gdppc_1990_stm_5c v2x_clphy_5_c v2exl_legitideol_5_c gini_disp_c s_far_Maddison_pop_estimatec) (v2x_polyarchy v2x_polyarchy_5 e_migdppcln s_far_Maddison_gdppc_1990_stm_5 v2x_clphy_5 v2exl_legitideol_5 gini_disp s_far_Maddison_pop_estimate)

sum (v2x_polyarchy v2x_polyarchy_5 e_migdppcln s_far_Maddison_gdppc_1990_stm_5 v2x_clphy_5 v2exl_legitideol_5 gini_disp s_far_Maddison_pop_estimate)


* Model 4 - repression, Robustness Test 1

	set more off
	mi estimate, post: xtmixed  red_pref age c.time_under_autocracy##c.v2x_clphy_5 v2x_polyarchy_5 s_far_Maddison_gdppc_1990_stm_5 ///
	v2x_polyarchy e_migdppcln gini_disp s_far_Maddison_pop_estimate ///
	i.sex i.education_3 i.unemployed i.country_id i.dataset || country_name: R.cohortmatch5_15 || year: , var ml
	estimates store m4
	
outreg2 using R1_Table2MI.doc, replace ctitle(Model 4)
	
* FIGURE XX.A

sum v2x_clphy_5

estimates restore m4
	mimrgns ,  at(time_under_autocracy=(0 10 20 30 40 50 60) v2x_clphy_5=(-0.6125176 0 0.6125176)) atmeans cmdmargins	
	*cmdmargins is necessarcy to use marginsplots afterwards 
	marginsplot,	xlabel(0 (10) 60,  angle(horizontal)labsize(small))   ///
	ylabel(38(1)48 ,  angle(horizontal)labsize(small))   ///
	ytitle("Marginal effect of Time under autocracy" "on redistribution preferences", size(medsmall)) ///
	xtitle("time under autocracy  (at c)", size(medsmall))  ///
	legend(order(4 "-2 SD"  5 "Mean" 6 "+2 SD" ) position(6) ring(0) )  ///
	title("A. Time under Autocracy by Repression (at c) on preferences for redistribution.", size(medium)) ///
	scheme(plotplain) graphregion(fcolor(white)) name(fig4d, replace)
	
	graph save Output/R1_FigureM1, replace 
	graph export Output/R1_Figure1.pdf, replace

	
* Model 5 - ideology

	set more off
	mi estimate, post: xtmixed  red_pref age c.time_under_autocracy##c.v2exl_legitideol_5 v2x_polyarchy_5 s_far_Maddison_gdppc_1990_stm_5 ///
	v2x_polyarchy e_migdppcln gini_disp s_far_Maddison_pop_estimate ///
	i.sex i.education_3 i.unemployed i.country_id i.dataset || country_name: R.cohortmatch5_15 || year: , var ml
	estimates store m5

outreg2 using R1_Table3.doc, replace ctitle(Model 5)


* FIGURE XX.B
sum v2exl_legitideol_5

estimates restore m5
	mimrgns ,  at(time_under_autocracy=(0 10 20 30 40 50 60) v2exl_legitideol_5=(-2.815265 0 3.418735)) atmeans cmdmargins	
	marginsplot,	xlabel(0 (10) 60,  angle(horizontal)labsize(small))   ///
	ylabel(40(1)50 ,  angle(horizontal)labsize(small))   ///
	ytitle("Marginal effect of Time under autocracy" "on redistribution preferences", size(medsmall)) ///
	xtitle("time under autocracy", size(medsmall))  ///
	legend(order(4 "-Min"  5 "Mean" 6 "Max" ) position(12) ring(0) )  ///
	title("A. Time under Autocracy by Indoctrination (at c) on preferences for redistribution.", size(medium)) ///
	scheme(plotplain) graphregion(fcolor(white)) name(fig4e, replace)
	
	graph save Output/R1_FigureM2, replace
	graph export Output/R1_Figure2.pdf, replace


	
* Model 6 - ideology + repression interaction

	set more off
	mi estimate, post: xtmixed  red_pref age c.time_under_autocracy##c.v2x_clphy_5##c.v2exl_legitideol_5 v2x_polyarchy_5 s_far_Maddison_gdppc_1990_stm_5 ///
	v2x_polyarchy e_migdppcln gini_disp s_far_Maddison_pop_estimate ///
	i.sex i.education_3 i.unemployed i.country_id i.dataset || country_name: R.cohortmatch5_15 || year: , var ml
	estimates store m6

outreg2 using R1_Table4.doc, append ctitle(Model 6)

 estimates restore m6
	mimrgns, at(time_under_autocracy = (0 10 20 30 40 50 60) v2exl_legitideol_5=(-2.815265 0 3.418735) v2x_clphy_5=(-0.6125176 0 0.6125176)) atmeans cmdmargins
	marginsplot, by(v2exl_legitideol_5) ///
	xlabel(0 (10)60, angle(horizontal)labsize(small))   ///
	ylabel(40(1)50 ,  angle(horizontal)labsize(small))   ///
	ytitle("Marginal effect of indoctrination (at c)" "on redistribution preferences", size(medsmall)) ///
	xtitle("time under autocracy", size(medsmall))  ///
	legend(order( 4 "-2 SD"  5 "Mean" 6 "+2 SD" ) position(6) ring(0) )  ///
	title("", size(medium)) ///
	scheme(plotplain) graphregion(fcolor(white)) name(fig4f, replace)
	
	graph save Output/R1_FigureM3, replace
	graph export Output/R1_Figure3.pdf, replace

